<template>
  <div>
    <div style="margin-bottom: 20px;font-size: medium;font-weight: 900;">单位信息</div>
    <!--    输入框区域-->
    <div class="search">
      <el-form label-width="90px" :inline="true" :model="form" ref="formRef" :rules="rules">
        <el-form-item label="参与单位" prop="unitId">
          <el-input v-model="form.unitId" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="申报日期" prop="deTime">
          <el-date-picker type="date" placeholder="yyyy/mm/日" v-model="form.deTime"
            style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-form>
    </div>

    <div class="operation">
      <div style="font-size: medium;font-weight: 900;">工程明细
        <el-button plain @click="handleAdd"
          style="margin-left:20px;font-size: 12px; line-height: 8px; color: #409EFF;">新增</el-button>
      </div>
    </div>

    <!--表单区域-->
    <div class="table">
      <el-table :data="tableData" stripe border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="manifestId" label="序号" width="100" align="center" sortable></el-table-column>
        <el-table-column prop="manifestName" label="工作内容"></el-table-column>
        <el-table-column prop="measure" label="计量单位"></el-table-column>
        <el-table-column prop="quantity" label="工程量"></el-table-column>
        <el-table-column prop="engineeringQuantityCompleted" label="本期完成工程量"></el-table-column>
        <el-table-column prop="engineeringQuantityRemaining" label="剩余工程量"></el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template v-slot="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="del(scope.row.manifestId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--    上传照片-->
    <div class="upload">
      <el-form label-width="80px" :inline="true" style="margin-top: 20px;">
        <!--   上传凭证-->
        <el-form-item label="上传凭证" prop="image">
          <el-upload name="image" class="avatar-uploader" :action="$baseUrl + '/upload'"
            :headers="{ token: user.token }" list-type="picture" :on-success="handleAvatarSuccess">
            <el-button icon="el-icon-upload"></el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="label">支持文件格式: doc,docx,pdf,jpg,png,xlsx,xls,rar,zip</div>
    </div>

    <!-- 新建工程对话框 -->
    <el-dialog title="工程明细" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
      <el-form label-width="100px" style="padding-right: 50px" :model="form" :rules="rules" ref="formRef">
        <el-form-item prop="manifestName" label="工作内容">
          <el-input v-model="form.manifestName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="measure" label="计量单位">
          <el-input v-model="form.measure" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="quantity" label="工程量">
          <el-input v-model="form.quantity" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="engineeringQuantityCompleted" label="本期完成工程量">
          <el-input v-model="form.engineeringQuantityCompleted" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="engineeringQuantityRemaining" label="剩余工程量">
          <el-input v-model="form.engineeringQuantityRemaining" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="fromVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 底部区域 -->
    <span slot="footer">
      <el-button type="primary" style="margin-top: 360px;margin-left:700px" @click="save1">保 存</el-button>
      <router-link to="/salary"><el-button style="margin-left:30px">取 消</el-button></router-link>
    </span>
  </div>
</template>

<script>
export default {
  name: "AddProject",
  data() {
    return {
      props: ["unitId"],
      unitId: this.$route.query.param.unitId,
      projectId: this.$route.query.param.projectId,
      xianshi: this.$route.query.param.total,
      tableData: [],  // 所有的数据
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      name: null,
      fromVisible: false,
      // ruleForm:{},
      form: {
        unitId: this.$route.query.param.unitId,
        projectId: this.$route.query.param.projectId,
        projectName: this.$route.query.param.projectName,
        deTime: this.$route.query.param.deTime,
      },
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
        unitId: [
          { required: true, message: '请输入单位名称', trigger: 'blur' },
        ],
        projectName: [
          { required: true, message: "请输入项目名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" }
        ],
        deTime: [
          { required: true, message: '请选择申报日期', trigger: 'blur' },
        ],
        // engineeringQuantityCompleted:[{
        //   min:0,max:this.quantity,message:'已完成工程量数值不能大于总工程量',trigger:'blur'
        // }],
        // engineeringQuantityRemaining:[{
        //   min:0,max:this.quantity,message:'剩余工程量数值不能大于总工程量',trigger:'blur'
        // }],
      },
    }
  },
  created() {
    this.load(1)
  },
  methods: {
    handleAdd() {   // 新增数据
      // this.form = {
      //
      // }  // 新增数据的时候清空数据
      this.fromVisible = true   // 打开弹窗
    },
    handleEdit(row) {   // 编辑数据
      this.form = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.fromVisible = true   // 打开弹窗
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.form.projectId = this.projectId;
          this.$request({
            url: this.form.manifestId ? '/projects/manifest' : '/projects/manifest/insert',
            method: this.form.manifestId ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    save1() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.form.unitId = this.unitId
          this.form.projectName = this.projectName
          console.log("kkk");
          console.log(this.unitId);
          if (this.unitId != null) {
            console.log("mmmmm");
            this.$request.put("/projects", this.form).then((res) => {
              if (res.code === "200") {
                this.$message.success("保存成功");
                console.log("mmmmm");
                this.$router.push({
                  name: "Salary",
                });
              } else {
                this.$message.error(res.msg);
              }
            });
          }else {
            console.log("hh");
            console.log(this.unitId);
            console.log(this.form);
            console.log(this.tableData);
            this.$request
              .post("/projects/insert", this.form)
              .then((res) => {
                if (res.code === "200") {
                  this.$message.error("保存失败");
                } else {
                  this.$message.success("保存成功");

                  this.$router.push({
                    name: "Salary",
                  });
                }
              });
          }
        }
      });
    },
    del(id) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', { type: "warning" }).then(response => {
        this.$request.delete('/projects/manifest/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)   //  [1,2]
    },
    load(pageNum) {  // 分页查询
      var this_projectId = this.projectId;
      if (this.projectId)
        this.$request.get("/projects/" + this_projectId).then((res) => {
          this.tableData = res.data?.manifestList
          this.total = res.data?.total
        })
      console.log(this.tableData)
    },
  handleCurrentChange(pageNum) {
    this.load(pageNum)
  },
  handleAvatarSuccess(response) {
    this.form.image = response.data
  },
}
}
</script>

<style scoped>
.label {
  color: #409EFF;
  font-size: 10px;
}
</style>
